package com.glsc.ngateway.opmanage.controller;

import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.api.platform.dto.StatisticsInterfaceDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.TRPARequest;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.Visit;
import com.glsc.ngateway.opmanage.service.RPAService;
import com.glsc.ngateway.opmanage.service.ShortMsgService;
import com.glsc.ngateway.opmanage.service.StatisticsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/data")
public class StatisticsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private StatisticsService statisticsService;

    @Autowired
    public void setStatisticsService(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    @Autowired
    private ShortMsgService shortMsgService;
    @Autowired
    private RPAService rpaService;

    @GetMapping("/getRPAStatistics")
    @ApiOperation("查询RPA流程发送状态")
    public PlatformResponse<Page<TRPARequest>> getRPAStatistics(
            @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {

        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = PageConstant.PageSize;
        }
        Page<TRPARequest> page = rpaService.getAllRequestList(pageNo, pageSize);
        return PlatformResponse.successData(page);
    }


    @GetMapping("/getInterfaceStatistics")
    @ApiOperation("统计模块调用")
    public PlatformResponse<?> getVisitByDate(@RequestParam(value = "module", defaultValue = "", required = false) String module,
                                              @RequestParam(value = "beginDate", defaultValue = "", required = false) String beginDate,
                                              @RequestParam(value = "endDate", defaultValue = "", required = false) String endDate) {

        if (beginDate.equals("")) {
            beginDate = null;
        }
        if (endDate.equals("")) {
            endDate = null;
        }

        //module  短信平台 OA对接 Flowable 传真 eHR
        List<String> moduleList = new ArrayList<>();
        moduleList.add("短信平台");
        moduleList.add("OA对接");
        moduleList.add("Flowable");
        moduleList.add("eHR");
        moduleList.add("传真");
        moduleList.add("platform");
        List<StatisticsInterfaceDto> list = new ArrayList<>();
        Integer count = 0;

        if (module.equals("")) {
            StatisticsInterfaceDto sdto = new StatisticsInterfaceDto();
            count = shortMsgService.statShortMsgByDate(beginDate, endDate);
            sdto.setModule("短信平台");
            sdto.setInvokeNumber(count);
            list.add(sdto);

            StatisticsInterfaceDto oadto = new StatisticsInterfaceDto();
            count = statisticsService.statOAByDate(beginDate, endDate);
            oadto.setModule("OA对接");
            oadto.setInvokeNumber(count);
            list.add(oadto);

            StatisticsInterfaceDto flowdto = new StatisticsInterfaceDto();
            count = statisticsService.statModuleByDate("flowable", beginDate, endDate);
            flowdto.setModule("Flowable");
            flowdto.setInvokeNumber(count);
            list.add(flowdto);

            StatisticsInterfaceDto ehrdto = new StatisticsInterfaceDto();
            count = statisticsService.statModuleByDate("ehr", beginDate, endDate);
            ehrdto.setModule("eHR");
            ehrdto.setInvokeNumber(count);
            list.add(ehrdto);

            {
                StatisticsInterfaceDto statisticsInterfaceDto = new StatisticsInterfaceDto();
                module = "platform";
                count = statisticsService.statModuleByDate(module, beginDate, endDate);
                statisticsInterfaceDto.setModule("platform");
                statisticsInterfaceDto.setInvokeNumber(count);
                list.add(statisticsInterfaceDto);
            }
        } else if (module.equals("短信平台")) {
            StatisticsInterfaceDto dto = new StatisticsInterfaceDto();
            count = shortMsgService.statShortMsgByDate(beginDate, endDate);
            dto.setModule("短信平台");
            dto.setInvokeNumber(count);
            list.add(dto);
        } else if (module.equals("OA对接")) {
            StatisticsInterfaceDto dto = new StatisticsInterfaceDto();
            count = statisticsService.statOAByDate(beginDate, endDate);
            dto.setModule("OA对接");
            dto.setInvokeNumber(count);
            list.add(dto);
        } else if (module.equals("Flowable")) {
            StatisticsInterfaceDto dto = new StatisticsInterfaceDto();
            module = "flowable";
            count = statisticsService.statModuleByDate(module, beginDate, endDate);
            dto.setModule("Flowable");
            dto.setInvokeNumber(count);
            list.add(dto);

        } else if (module.equals("eHR")) {
            StatisticsInterfaceDto dto = new StatisticsInterfaceDto();
            module = "ehr";
            count = statisticsService.statModuleByDate(module, beginDate, endDate);
            dto.setModule("eHR");
            dto.setInvokeNumber(count);
            list.add(dto);

        } else if (module.equals("platform")) {
            StatisticsInterfaceDto dto = new StatisticsInterfaceDto();
            module = "platform";
            count = statisticsService.statModuleByDate(module, beginDate, endDate);
            dto.setModule("platform");
            dto.setInvokeNumber(count);
            list.add(dto);
        } else {
            return PlatformResponse.success();
        }

        return PlatformResponse.successData(list);
    }


    @GetMapping("/getVisitByDate")
    @ApiOperation("统计单日总访问次数")
    public PlatformResponse<List<Map>> getVisitByDate(
            @ApiParam(value = "查询日期") @RequestParam(value = "date", defaultValue = "", required = false) String date) {

        List<Map> list = statisticsService.countByDate(date);
        return PlatformResponse.successData(list);
    }

    // 暂时不用
    @GetMapping("/getVisitByUser")
    @ApiOperation("统计系统访问次数")
    public PlatformResponse<List<Map>> getVisitByUser(
            @ApiParam(value = "系统名称") @RequestParam("sysName") String sysName,
            @ApiParam(value = "查询日期") @RequestParam(value = "date", defaultValue = "", required = false) String date) {

        List<Map> list = statisticsService.countByUser(sysName, date);
        return PlatformResponse.successData(list);
    }


    @GetMapping("/getVisitByMonth")
    @ApiOperation("统计单月总访问次数")
    public PlatformResponse<List<Map>> getVisitByMonth(
            @ApiParam(value = "查询月份") @RequestParam(value = "month", defaultValue = "", required = false) String month) {

        List<Map> list = statisticsService.countByMonth(month);
        return PlatformResponse.successData(list);
    }

    // 暂时不用
    @GetMapping("/monthDataByUser")
    @ApiOperation("统计系统访问次数")
    public PlatformResponse<List<Map>> monthDataByUser(
            @ApiParam(value = "系统名称") @RequestParam("sysName") String sysName,
            @ApiParam(value = "查询月份") @RequestParam(value = "month", defaultValue = "", required = false) String month) {

        List<Map> list = statisticsService.countMonthBySys(sysName, month);
        return PlatformResponse.successData(list);
    }


    @GetMapping("/getVisitByYear")
    @ApiOperation("统计年度总访问次数")
    public PlatformResponse<Map<String, List<Long>>> getVisitByYear(
            @ApiParam(value = "查询年份") @RequestParam(value = "year", defaultValue = "", required = false) String year) {

        Map<String, List<Long>> list = statisticsService.countByYear(year);
        return PlatformResponse.successData(list);
    }


    @GetMapping("/getLoginData")
    @ApiOperation("查询当前用户状态")
    public PlatformResponse<Page<Visit>> getLoginData(
            @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {


        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = PageConstant.PageSize;
        }

        Page<Visit> page = statisticsService.getLoginData(pageNo, pageSize);
        return PlatformResponse.successData(page);
    }


}
